package com.example.springboot_jdk8.request;

import java.util.concurrent.CompletableFuture;

public class ExcutePipline {

    public RequestPipeline requestPipeline;

    public void initPipeline(RequestPipeline requestPipeline) {
        this.requestPipeline = requestPipeline;
    }


    public void excutePipline() {
        CompletableFuture completableFuture = null;
        RequestNode cur = requestPipeline.head;
        while (cur != null) {
            //当前api是否要异步
            if (cur.api.isAsync()) {
                RequestNode finalCur = cur;
                completableFuture = CompletableFuture.supplyAsync(()-> finalCur.api.request());
            }else {
                RequestNode finalCur1 = cur;
                if (completableFuture == null) {
                    completableFuture = new CompletableFuture().thenApply((ok)-> finalCur1.api.request());
                }else {
                    completableFuture.thenApply((preResult) -> finalCur1.api.request());
                }

            }



            cur = cur.next;
        }


    }
}
